//SPDX-FileCopyrightText: Copyright 2022-2024 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

package api_common

import (
	"context"
	"math"

	"atomgit.com/openlinksaas-org/api-server/dao/fs_dao"
	"atomgit.com/openlinksaas-org/api-server/dao/idea_dao"
	"atomgit.com/openlinksaas-org/api-server/dao/project_dao"
	"atomgit.com/openlinksaas-org/proto-gen-go.git/fs_api"
	"github.com/google/uuid"
)

func AddProject(ctx context.Context, projectId, projectName, projectDesc, createUserId string, nowTime int64) error {
	//init counter
	err := project_dao.ProjectCounterDao.Insert(ctx, projectId, project_dao.COUNTER_KEY_ISSUE_ID)
	if err != nil {
		return err
	}

	//创建默认和管理员角色
	roleId := uuid.NewString()
	err = project_dao.MemberRoleDao.Insert(ctx, &project_dao.RoleInfoDbItem{
		RoleId:    roleId,
		ProjectId: projectId,
		BasicInfo: project_dao.BasicRoleInfoDbItem{
			RoleName: "普通成员",
			Admin:    false,
		},
		CreateTime:  nowTime,
		UpdateTime:  nowTime,
		DefaultRole: true,
	})
	if err != nil {
		return err
	}
	adminRoleId := uuid.NewString()
	err = project_dao.MemberRoleDao.Insert(ctx, &project_dao.RoleInfoDbItem{
		RoleId:    adminRoleId,
		ProjectId: projectId,
		BasicInfo: project_dao.BasicRoleInfoDbItem{
			RoleName: "管理员",
			Admin:    true,
		},
		CreateTime:  nowTime,
		UpdateTime:  nowTime,
		DefaultRole: false,
	})
	if err != nil {
		return err
	}
	//创建项目相关文件存储
	projectFsId := uuid.NewString()
	err = fs_dao.FsInfoDao.Insert(ctx, &fs_dao.FsInfoDbItem{
		FsId:      projectFsId,
		OwnerType: int32(fs_api.FS_OWNER_TYPE_FS_OWNER_TYPE_PROJECT),
		OwnerId:   projectId,
		Status:    fs_dao.FsStatusDbItem{},
	})
	if err != nil {
		return err
	}
	docFsId := uuid.NewString()
	err = fs_dao.FsInfoDao.Insert(ctx, &fs_dao.FsInfoDbItem{
		FsId:      docFsId,
		OwnerType: int32(fs_api.FS_OWNER_TYPE_FS_OWNER_TYPE_PROJECT),
		OwnerId:   projectId,
		Status:    fs_dao.FsStatusDbItem{},
	})
	if err != nil {
		return err
	}

	issueFsId := uuid.NewString()
	err = fs_dao.FsInfoDao.Insert(ctx, &fs_dao.FsInfoDbItem{
		FsId:      issueFsId,
		OwnerType: int32(fs_api.FS_OWNER_TYPE_FS_OWNER_TYPE_PROJECT),
		OwnerId:   projectId,
		Status:    fs_dao.FsStatusDbItem{},
	})
	if err != nil {
		return err
	}

	requireMentFsId := uuid.NewString()
	err = fs_dao.FsInfoDao.Insert(ctx, &fs_dao.FsInfoDbItem{
		FsId:      requireMentFsId,
		OwnerType: int32(fs_api.FS_OWNER_TYPE_FS_OWNER_TYPE_PROJECT),
		OwnerId:   projectId,
		Status:    fs_dao.FsStatusDbItem{},
	})
	if err != nil {
		return err
	}
	ideaFsId := uuid.NewString()
	err = fs_dao.FsInfoDao.Insert(ctx, &fs_dao.FsInfoDbItem{
		FsId:      ideaFsId,
		OwnerType: int32(fs_api.FS_OWNER_TYPE_FS_OWNER_TYPE_PROJECT),
		OwnerId:   projectId,
		Status:    fs_dao.FsStatusDbItem{},
	})
	if err != nil {
		return err
	}
	bulletinFsId := uuid.NewString()
	err = fs_dao.FsInfoDao.Insert(ctx, &fs_dao.FsInfoDbItem{
		FsId:      bulletinFsId,
		OwnerType: int32(fs_api.FS_OWNER_TYPE_FS_OWNER_TYPE_PROJECT),
		OwnerId:   projectId,
		Status:    fs_dao.FsStatusDbItem{},
	})
	if err != nil {
		return err
	}

	apiCollFsId := uuid.NewString()
	err = fs_dao.FsInfoDao.Insert(ctx, &fs_dao.FsInfoDbItem{
		FsId:      apiCollFsId,
		OwnerType: int32(fs_api.FS_OWNER_TYPE_FS_OWNER_TYPE_PROJECT),
		OwnerId:   projectId,
		Status:    fs_dao.FsStatusDbItem{},
	})
	if err != nil {
		return err
	}

	pagesFsId := uuid.NewString()
	err = fs_dao.FsInfoDao.Insert(ctx, &fs_dao.FsInfoDbItem{
		FsId:      pagesFsId,
		OwnerType: int32(fs_api.FS_OWNER_TYPE_FS_OWNER_TYPE_PROJECT),
		OwnerId:   projectId,
		Status:    fs_dao.FsStatusDbItem{},
	})
	if err != nil {
		return err
	}

	testCaseFsId := uuid.NewString()
	err = fs_dao.FsInfoDao.Insert(ctx, &fs_dao.FsInfoDbItem{
		FsId:      testCaseFsId,
		OwnerType: int32(fs_api.FS_OWNER_TYPE_FS_OWNER_TYPE_PROJECT),
		OwnerId:   projectId,
		Status:    fs_dao.FsStatusDbItem{},
	})
	if err != nil {
		return err
	}

	testResultFsId := uuid.NewString()
	err = fs_dao.FsInfoDao.Insert(ctx, &fs_dao.FsInfoDbItem{
		FsId:      testResultFsId,
		OwnerType: int32(fs_api.FS_OWNER_TYPE_FS_OWNER_TYPE_PROJECT),
		OwnerId:   projectId,
		Status:    fs_dao.FsStatusDbItem{},
	})
	if err != nil {
		return err
	}

	defaultIdeaGroupId := uuid.NewString()
	err = idea_dao.IdeaGroupDao.Insert(ctx, &idea_dao.IdeaGroupDbItem{
		IdeaGroupId:  defaultIdeaGroupId,
		Name:         "项目知识点",
		ProjectId:    projectId,
		Weight:       math.MaxUint32,
		IdeaCount:    0,
		CreateUserId: createUserId,
		UpdateUserId: createUserId,
		CreateTime:   nowTime,
		UpdateTime:   nowTime,
	})
	if err != nil {
		return err
	}

	//创建项目
	projectItem := &project_dao.ProjectInfoDbItem{
		ProjectId: projectId,
		BasicInfo: project_dao.BasicProjectInfoDbItem{
			ProjectName: projectName,
			ProjectDesc: projectDesc,
		},
		CreateTime:         nowTime,
		UpdateTime:         nowTime,
		Closed:             false,
		OwnerUserId:        createUserId,
		DefaultRoleId:      roleId,
		IssueFsId:          issueFsId,
		ProjectFsId:        projectFsId,
		DocFsId:            docFsId,
		RequireMentFsId:    requireMentFsId,
		IdeaFsId:           ideaFsId,
		BulletinFsId:       bulletinFsId,
		ApiCollFsId:        apiCollFsId,
		PagesFsId:          pagesFsId,
		TestCaseFsId:       testCaseFsId,
		TestResultFsId:     testResultFsId,
		DefaultIdeaGroupId: defaultIdeaGroupId,
		Setting: project_dao.SettingDbItem{
			HideCustomEvent:          false,
			HideCustomEventForAdmin:  false,
			ShowRequireMentListEntry: true,
			ShowTaskListEntry:        true,
			ShowBugListEntry:         true,
			ShowTestCaseListEntry:    true,
		},
		TipList: []string{},
	}
	err = project_dao.ProjectInfoDao.Insert(ctx, projectItem)
	if err != nil {
		return err
	}
	//增加项目成员
	err = AddMember(ctx, nowTime, projectItem, createUserId, adminRoleId)
	if err != nil {
		return err
	}
	return nil
}
